package com.bci.pwtz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.BatchUpdate;
import com.bci.pwtz.service.BatchUpdateService;
import com.bci.pwtz.service.impl.BusinessCommonServiceImpl;

@Controller
public class BatchUpdateController extends AbstractController{
	@Autowired
	private BatchUpdateService service;
	
	@RequestMapping(value="/user/batchupdate")
	@ResponseBody
	public Map<String, String> batchupdate(HttpServletRequest request,String[] deviceid,String[] devicename,String devicetype){
		Map<String, String> map = new HashMap<String, String>();
		String[] chooseColumns = request.getParameterValues("chooseColumn");
		
		if(chooseColumns == null || chooseColumns.length==0){
			return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "请至少选中一条要修改的列");
		}
		if(deviceid == null || deviceid.length == 0){
			return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "修改设备不存在，请重新选择设备");
		}
		if(devicetype == null || devicetype.trim().length() == 0 || BusinessCommonServiceImpl.TABLE_NAME_MAP.get(PwtzUtils.passLong(devicetype)) == null){
			return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "修改设备类型不存在，请重新选择设备类型");
		}
		
		
		Map<String,String> columnAndVal = new HashMap<String, String>();
		for(String chooseColumn : chooseColumns){
			if(hashColumn(devicetype, chooseColumn)){
				String key = chooseColumn;
				String value = request.getParameter(chooseColumn);
				columnAndVal.put(key, value);
			}
		}
		
		if(columnAndVal.isEmpty()){
			return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "修改列不存在");
		}
		
		int result = 0;
		try {
			result = service.bacthUpdate(deviceid, PwtzUtils.passLong(devicetype), columnAndVal);
		} catch (Exception e) {
			_log.error("batchUpdate error:",e);
		}
		 if (result>0)
         {
			 for(int i =0;i<deviceid.length;i++){
				 setLog(deviceid[i], getLastCompanyId(), PwtzUtils.passInt(devicetype), AbstractController.update, "修改"+PwtzConstants.DEVICE_TYPE_MAP.get(new Long(devicetype))+"["+devicename[i].replace("~!@", "#")+"]成功", getUserId());
			 }
             map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "批量修改成功");
         }
         else
         {
             map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "批量修改失败");
         }
		return map;
	}
	
	private boolean hashColumn(String deviceTypeID,String column){
		if(deviceTypeID == null || deviceTypeID.trim().length() == 0 || column == null || column.trim().length() == 0){
			return false;
		}
		List<BatchUpdate> infos = PwtzConstants.BATCH_UPDATE.get(deviceTypeID);
		if(infos == null || infos.isEmpty()){
			return false;
		}
		for(BatchUpdate info : infos){
			if(column.equals(info.getColumn())){
				return true;
			}
		}
		return false;
	}
}
